package com.dianrui.fmi.activity;

import android.app.ProgressDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetPasswordFinalActivity extends BaseActivity {
    private ProgressDialog pd;
    private String mail;
    private String code;
    boolean isHide = false;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.login_password)
    EditText loginPassword;
    @BindView(R.id.current_pwd_eyes)
    ImageView currentPwdEyes;


    @BindView(R.id.save_btn)
    Button saveBtn;

    @Override
    public int getContentViewId() {
        return R.layout.activity_forgetpasswordfinal;
    }

    @Override
    public void initView() {
        pd = new ProgressDialog(this);
        if (getIntent() != null && getIntent().getExtras() != null) {
            mail = getIntent().getStringExtra("email");
            code = getIntent().getStringExtra("verifyCode");
        }
        title.setText(getString(R.string.forget_password));


        loginPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (loginPassword.length() > 0) {
                    saveBtn.setBackgroundResource(R.drawable.login_orange_shape);
                } else {
                    saveBtn.setBackgroundResource(R.drawable.gray_shape);
                }
            }
        });

        currentPwdEyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                    currentPwdEyes.setImageResource(R.mipmap.logo_eyes_open);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    loginPassword.setTransformationMethod(method);
                    loginPassword.setSelection(loginPassword.length());
                } else {
                    isHide = true;
                    currentPwdEyes.setImageResource(R.mipmap.logo_eyes_close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    loginPassword.setTransformationMethod(method);
                    loginPassword.setSelection(loginPassword.length());
                }
            }
        });

    }


    @OnClick({R.id.back, R.id.save_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save_btn:
                if (StringUtils.isEmpty(loginPassword.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.please_input_your_password));
                } else {
                    forgetPassword();
                }
                break;
            default:
                break;
        }
    }

    private void forgetPassword() {
        pd.setMessage(getString(R.string.submit));
        pd.show();
        pd.setCancelable(false);
        pd.setCanceledOnTouchOutside(false);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("email", mail);
        jsonObject.addProperty("type", "1");
        jsonObject.addProperty("verify_code", code);
        jsonObject.addProperty("password", loginPassword.getText().toString());
        OkHttpUtils.getInstance().postWithMemberToken(Url.FORGETPWD, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                pd.dismiss();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpLoginActivitys(LoginActivity.class);
                                finish();
                            } else {
                                pd.dismiss();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    pd.dismiss();
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }
}
